<template>
  <el-dialog
    v-el-drag-dialog
    title="修改安检项明细"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="left" label-width="80px">
      <el-form-item label-width="80" label="安检项类型:" prop="itemId" style="width: 100%;">
        <el-select v-model="temp.itemId" placeholder="请选择">
          <el-option
            v-for="(i, ind) in checkTypeList"
            :key="ind"
            :label="i.itemName"
            :value="i.itemId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="80" style="width: 100%;" label="安检项明细:" prop="itemDetailName">
        <el-input v-model="temp.itemDetailName" type="textarea" placeholder="请输入内容" maxlength="100" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { checkNapeConfig, checkNapeUpdate } from '@/api'

  export default {
    name: 'editinfo',
    data() {
      return {
        visible: false,
        checkTypeList: [],
        temp: {
          itemDetailName: '',
          itemId: '',
          itemDetailId: ''
        },
        rules: {
          itemId: [{ required: true, message: '请选择安检项类型', trigger: 'change' }],
          itemDetailName: [{ required: true, message: '请输入内容', trigger: 'change' }]
        }
      }
    },
    async created() {
      await this.getTreeData()
    },
    methods: {
      open(val) {
        console.log('编辑val', val)
        this.temp.itemDetailId = val.itemDetailId
        this.temp.itemId = val.itemId
        this.temp.itemDetailName = val.itemDetailName
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      success() {
        console.log('this.temp', this.temp)
        this.$refs.dataForm.validate(valid => {
          console.log('校验', valid)
          if (!valid) return
          this.add()
        })
      },
      async add() {
        const res = await checkNapeUpdate(this.temp)
        console.log('编辑res', res)
        this.visible = false
        if (res.status === 200) {
          this.$notify({
            title: '提示',
            message: `编辑成功`,
            type: 'success',
            duration: 2000
          })
          this.$emit('editUpData')
        }
      },
      reset() {
        this.temp = {
          itemDetailName: '',
          itemId: '',
          itemDetailId: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      },
      // 获取安检项配置
      async getTreeData() {
        const res = await checkNapeConfig({})
        this.checkTypeList = res.data
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
}
</style>
